package com.elong.android.specialhouse.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.ApartmentAPI;
import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.JSONConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.adapter.common.BaseViewHolder;
import com.elong.android.specialhouse.adapter.common.PowerAdapter;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.entity.response.OrderListLogVo;
import com.elong.android.specialhouse.entity.response.OrderLogResp;
import com.elong.android.specialhouse.utils.ToastUtils;
import com.elong.framework.netmid.ElongRequest;
import com.elong.framework.netmid.request.RequestOption;
import com.elong.framework.netmid.response.IResponse;
import com.elong.framework.netmid.response.StringResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * 类型功能说明描述
 * 
 * @author cheng.wang
 * @date 2015/9/8 16:15
 */
public class OrderLogActivity extends BaseVolleyActivity<IResponse<?>>
		implements AdapterView.OnItemClickListener {

	private ListView lvOrderLog;
	private TextView tvOrderNumber;
	private PowerAdapter<OrderListLogVo> mPowerAdapter;
	private ArrayList<OrderListLogVo> mDataList;
	private String mOrderId;
	private OrderLogResp mOrderLogResp;

	@Override
	protected void initContentView() {
		setContentView(R.layout.act_order_log);
		setHeader(R.string.title_order_log);
		lvOrderLog = (ListView) findViewById(R.id.lv_order_log_list);
		tvOrderNumber = (TextView) findViewById(R.id.tv_order_number);
		lvOrderLog.setOnItemClickListener(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		intData();
		getOrderLogList();
	}

	private void intData() {
		mDataList = new ArrayList<OrderListLogVo>();
		mOrderId = getIntent().getStringExtra(AppConstants.ORDER_ID);

	}

	public void getOrderLogList() {
		RequestOption req = new RequestOption();
		JSONObject params = new JSONObject();
		params.put(JSONConstants.ATTR_ORDER_ID, mOrderId);
		req.setJsonParam(params);
		requestHttp(req, ApartmentAPI.getOrderLog, StringResponse.class, true);
	}

	@Override
	public void onTaskPost(ElongRequest request, IResponse<?> t) {
		super.onTaskPost(request, t);

		ApartmentAPI action = (ApartmentAPI) request.getRequestOption()
				.getHusky();
		JSONObject result = JSON.parseObject(((StringResponse) t).getContent());
		if (result == null) {
			ToastUtils.show(this, R.string.server_error);
			return;
		}
		// 过滤错误信息
		if (handleIsError(request, result)) {
			return;
		}
		// 返回成功
		switch (action) {
		case getOrderLog:
			inflateListData(t);
			break;
		}

	}

	private void inflateListData(IResponse<?> t) {
		if (t == null)
			return;
		mOrderLogResp = JSON.parseObject(((StringResponse) t).getContent(),
				OrderLogResp.class);
		if (mOrderLogResp == null)
			return;
		if (mOrderLogResp.list == null || mOrderLogResp.list.size() <= 0)
			return;

		mDataList = mOrderLogResp.list;

		tvOrderNumber.setText("订单号：" + mDataList.get(0).orderId);

		mPowerAdapter = new PowerAdapter<OrderListLogVo>(this,
				R.layout.item_order_log_list, mDataList) {
			@Override
			protected void convert(BaseViewHolder holder, OrderListLogVo item) {
				resetItemStyle(holder, item);
				// 显示操作时间
				showOperateTime(holder, item.opTime);

				// 显示操作内容
				if (!TextUtils.isEmpty(item.opdetail)) {
					holder.setText(R.id.tv_detail, item.opdetail);
				}
			}
		};

		lvOrderLog.setDividerHeight(0);
		lvOrderLog.setAdapter(mPowerAdapter);
	}

	private void showOperateTime(BaseViewHolder holder, String opTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA);
		try {
			Date respDate = sdf.parse(opTime);
			SimpleDateFormat sdf2 = new SimpleDateFormat("H:mm:ss M月dd日",
					Locale.CHINA);
			String timeDate = sdf2.format(respDate);
			if (!TextUtils.isEmpty(opTime)) {
				holder.setText(R.id.tv_time_date, timeDate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	/**
	 * 重置item的样式
	 * 
	 * @param holder
	 */
	private void resetItemStyle(BaseViewHolder holder, OrderListLogVo item) {
		if (mDataList.size() - 1 == holder.getPosition()) {
			// 最后一个item,左边的线条只有上半段
			resetLeftLine(holder, true);
		} else {
			// 其他的item,左边的线条都是充满父亲的高度
			resetLeftLine(holder, false);
		}
		resetCircleAndBgStyle(holder, item);
	}

	/**
	 * 重置item中左边的线条
	 * 
	 * @param holder
	 * @param isLastItem
	 */
	private void resetLeftLine(BaseViewHolder holder, boolean isLastItem) {
		View leftLine = holder.getView(R.id.v_line_left);
		int lineWidth = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 2, getResources()
						.getDisplayMetrics());
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				lineWidth, FrameLayout.LayoutParams.MATCH_PARENT);
		if (isLastItem) {
			params.bottomMargin = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 75, getResources()
							.getDisplayMetrics());
		} else {
			params.bottomMargin = 0;
		}
		params.gravity = Gravity.CENTER_HORIZONTAL;
		leftLine.setLayoutParams(params);
	}

	/**
	 * 重置item中的圆圈和背景卡片的样式
	 * 
	 * @param holder
	 */
	private void resetCircleAndBgStyle(BaseViewHolder holder,
			OrderListLogVo item) {
		ImageView circle = holder.getView(R.id.iv_circle);
		FrameLayout bg = holder.getView(R.id.fl_bg_order_log);

		// 第一个item的圆圈和卡片背景是绿色的,其他item的圆圈和卡片背景是灰色的
		boolean isFirstItem = 0 == holder.getPosition();
		circle.setImageResource(isFirstItem ? R.drawable.icon_circle_green_big
				: R.drawable.icon_circle_grey_small);
		bg.setBackgroundResource(isFirstItem ? R.drawable.bg_order_log_green
				: R.drawable.bg_order_log_grey);
	}
}